﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ArchPostprocessing.CompoundStructureManage.Utility;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKArch.Utils;

namespace ArchPostprocessing.CompoundStructureManage.Forms
{
	public partial class TolerateSettingForm : System.Windows.Forms.Form
	{
		public TolerateSettingForm()
		{
			this.InitializeComponent();
		}

		public double TolerateSpace
		{
			get
			{
				return Common.MMToFeet(Convert.ToDouble(this.tbTolerateSpace.Text));
			}
		}

		public double TolerateAngle
		{
			get
			{
				return Common.MMToFeet(Convert.ToDouble(this.tbTolerateAngle.Text));
			}
		}

		private void TolerateSettingForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.ReadInfo();
		}

		private void TolerateSettingForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (DialogResult.Cancel == base.DialogResult)
			{
				return;
			}
			string text = "";
			if (!this.ValidatingParameters(ref text))
			{
				MessageBox.Show(text, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				e.Cancel = true;
				return;
			}
			this.SaveInfo();
		}

		public void ReadInfo(ref double tolerateSpace, ref double tolerateAngle)
		{
			string text = Path.Combine(Product.WorkingLocation, "TolerateSettingForm.xml");
			if (!File.Exists(text))
			{
				tolerateSpace = 0.0;
				tolerateAngle = 0.0;
				return;
			}
			ArchPostprocessing.CompoundStructureManage.Utility.XmlHelper2 xmlHelper = new ArchPostprocessing.CompoundStructureManage.Utility.XmlHelper2(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			string value = xmlHelper.GetValue("TolerateSpace");
			tolerateSpace = Common.MMToFeet(Convert.ToDouble(value));
			string value2 = xmlHelper.GetValue("TolerateAngle");
			tolerateAngle = Common.MMToFeet(Convert.ToDouble(value2));
		}

		private void SaveInfo()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = Path.Combine(workingLocation, "TolerateSettingForm.xml");
			ArchPostprocessing.CompoundStructureManage.Utility.XmlHelper2 xmlHelper = new ArchPostprocessing.CompoundStructureManage.Utility.XmlHelper2(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("TolerateSpace", Common.FeetToMM(this.TolerateSpace).ToString());
			xmlHelper.SetValue("TolerateAngle", Common.FeetToMM(this.TolerateAngle).ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void ReadInfo()
		{
			double val = 0.0;
			double val2 = 0.0;
			this.ReadInfo(ref val, ref val2);
			this.tbTolerateSpace.Text = Common.FeetToMM(val).ToString("f0");
			this.tbTolerateAngle.Text = Common.FeetToMM(val2).ToString("f0");
		}

		private void tbTolerateSpace_Validating(object sender, CancelEventArgs e)
		{
		}

		private void tbTolerateAngle_Validating(object sender, CancelEventArgs e)
		{
		}

		private bool ValidatingParameters(ref string message)
		{
			double num = 0.0;
			if (!TolerateSettingForm.ValidatingDouble(this.tbTolerateSpace.Text, TolerateSettingForm.ValidatingType.None, ref num))
			{
				message = "容差值(间距)参数不正确.";
				return false;
			}
			num = 0.0;
			if (!TolerateSettingForm.ValidatingDouble(this.tbTolerateAngle.Text, TolerateSettingForm.ValidatingType.None, ref num))
			{
				message = "容差值(角度)参数不正确.";
				return false;
			}
			return true;
		}

		public static bool ValidatingDouble(string strValue, TolerateSettingForm.ValidatingType validatingType, ref double dValue)
		{
			double num = 0.0;
			try
			{
				num = Convert.ToDouble(strValue);
			}
			catch (Exception)
			{
				return false;
			}
			if (validatingType == TolerateSettingForm.ValidatingType.GreaterThanZero && Geometry.GreaterThan(num, 0.0))
			{
				dValue = num;
				return true;
			}
			if (validatingType == TolerateSettingForm.ValidatingType.GreaterThanAndEqualZero && Geometry.Greaterthan_Or_Equal(num, 0.0))
			{
				dValue = num;
				return true;
			}
			if (validatingType == TolerateSettingForm.ValidatingType.Minus2PITo2PI && Geometry.GreaterThan(num, -360.0) && Geometry.LessThan(num, 360.0))
			{
				dValue = num;
				return true;
			}
			if (validatingType == TolerateSettingForm.ValidatingType.ZeroTo2PI && Geometry.Greaterthan_Or_Equal(num, 0.0) && Geometry.LessThan(num, 360.0))
			{
				dValue = num;
				return true;
			}
			if (validatingType == TolerateSettingForm.ValidatingType.None)
			{
				dValue = num;
				return true;
			}
			return false;
		}

		public enum ValidatingType
		{
			None,
			GreaterThanZero,
			GreaterThanAndEqualZero,
			Minus2PITo2PI,
			ZeroTo2PI
		}
	}
}
